<?php
namespace Compta\ChartsBundle\Entity;

/**
 * Description of DataHandler
 *
 * @package    ebook
 * @author     Valentin Clavreul <valentin.clavreul@smile.fr>
 * @version    SVN: $Id: DataHandler.php 6 2011-11-02 17:01:21Z vclavreul@gmail.com $
 */
class DataHandler
{
    protected $title = array();
    protected $colors = array();
    protected $data = array();

    public function addLine($line, $name=null)
    {
        if ( $name === null ) {
            $this->data[] = $line;
        }
        else {
            $this->data[$name] = $line;
        }
        return $this;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getNormalizedData($coef = 1)
    {
        $max = $this->getMax();
        $normalized = new DataHandler();
        foreach ( $this->data as $key => $line ) {
            $normalizedLine = array();
            foreach ( $line as $id => $data ) {
                $normalizedLine[$id] = $data * 100 * $coef / $max;
            }
            $normalized->addLine($normalizedLine, $key);
            $normalized->setColors($this->colors);
            $normalized->setTitle($this->title);
        }
        return $normalized;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    public function getColors()
    {
        return $this->colors;
    }

    public function setColors($colors)
    {
        $this->colors = $colors;
        return $this;
    }

    public function getMax()
    {
        $max = 0;
        foreach ( $this->getData() as $line ) {
            $sum = array_sum($line);
            if ( $sum > $max )
                $max = $sum;
        }
        return $max;
    }

    public function getAxisLegend()
    {
        return array_keys($this->getData());
    }

    public function loadMockData($nLines=4, $nCols=15)
    {
        $this->title = array();
        $this->colors = array();
        $this->data = array();


        for ( $i = 0; $i < $nCols; $i++ ) {
            $n = $nLines;
            $min = (int) 200 / $nLines;
            $max = (int) 1000 / $nLines;
            $line = array();
            for ( $i = 0; $i < $n; $i++ ) {
                $line[] = rand($min, $max);
            }
            $this->addLine($this->getRandomLine($line));
        }
        $this->setColors($this->interpolateColors('#446688', '#ccccee', $nLines));
        $this->setTitle(range(1, $nLines));
    }

    public function interpolateColors($minColor, $maxColor, $n=4)
    {
        $minR = substr($minColor, 1, 2);
        $maxR = substr($maxColor, 1, 2);
        $rouge = $this->interpolateHex($minR, $maxR, $n);
        $minV = substr($minColor, 3, 2);
        $maxV = substr($maxColor, 3, 2);
        $vert = $this->interpolateHex($minV, $maxV, $n);
        $minB = substr($minColor, 5, 2);
        $maxB = substr($maxColor, 5, 2);
        $bleu = $this->interpolateHex($minB, $maxB, $n);

        $colors = array();
        for ( $i = 0; $i < $n; $i++ ) {
            $color = '#' . $rouge[$i] . $vert[$i] . $bleu[$i];
            $colors[] = $color;
        }

        return $colors;
    }

    public function interpolateHex($minHex, $maxHex, $n=4)
    {
        $minDec = hexdec($minHex);
        $maxDec = hexdec($maxHex);
        $hexValues = array($minHex);
        for ( $i = 1; $i < ($n - 1); $i++ ) {
            $dec = (int) ($minDec + $i * ($maxDec - $minDec) / $n);
            $hexValues[] = dechex($dec);
        }
        $hexValues[] = $maxHex;

        return $hexValues;
    }

}